package demo1;

import java.util.Arrays;

//模拟一个栈
class Stack {
    int[] elem;
    int size;

    //给定构造函数默认数组的内存大小为10
    public Stack() {
        this.elem = new int[3];
    }

    //压栈
    public void push(int num) {
        //判断栈是不是满
        if (isFull()) {
            this.elem = Arrays.copyOf(this.elem,this.elem.length*2);
        }
        this.elem[size++] = num;
    }
    //出栈
    public int pop() throws EmptyException{
        //判断栈是不是空
        if (empty()) {
            throw new EmptyException("栈为空");
        }
        return this.elem[--size];
    }
    //获得栈顶元素
    public int peek() {
        //判断栈是不是空
        if (empty()) {
            throw new EmptyException("栈为空");
        }
        return this.elem[size - 1];
    }
    public int size() {
        return size;
    }
    //判断栈是不是满
    public boolean isFull() {
        if (elem.length == size) {
            return true;
        }
        return false;
    }
    //判断栈是不是为空
    public boolean empty() {
        return size == 0;
    }
}
public class Test {
    public static void main(String[] args) {
        Stack stack = new Stack();
        stack.push(121);
        stack.push(122);
        stack.push(123);
        stack.push(124);
        System.out.println(stack.pop());
        System.out.println(stack.peek());
        for (int i = stack.size(); i > 0; i--) {
            System.out.print(stack.pop() + " ");
        }

    }
}
